<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片面板-->
    <el-card class="box-card">
      <!-- 查询区域 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryInfo.name" clearable @clear="getGoodsList">
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="goAddPage">添加商品</el-button>
        </el-col>
      </el-row>

      <!-- 表格数据 -->
      <el-table :data="goodsList" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="商品名称" prop="name"  width="200px"></el-table-column>
        <el-table-column label="商品描述" prop="desc"></el-table-column>
        <el-table-column label="商品价格(元)" prop="price" width="100px"></el-table-column>
        <el-table-column label="创建时间" prop="crtTime" width="140px"></el-table-column>
        <el-table-column label="操作" width="130px">
          <template slot-scope="item">
            <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="removeById(item.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.num"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.page"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryInfo: {
        name: '',
        page: 1,
        num: 10
      },
      goodsList: [],
      total: 21
    }
  },
  created() {
    this.getGoodsList()
  },
  methods: {
    async getGoodsList() {
      // TODO 获取商品信息
      const { data: ret } = await this.$http.get('/item/list', {
        params: this.queryInfo
      })
      if (ret.rc !== 'S') this.$message.error('登录失败')
      this.goodsList = ret.data
    },
    goAddPage() {
      // 转到添加界面
      this.$router.push('/goods/add')
    },
    handleSizeChange(newSize) {
      // 更新数据
      console.log(newSize)
    },
    handleCurrentChange(newPage) {
      // 更新数据
      console.log(newPage)
    },
    removeById(id) {
      //
    }
  }
}
</script>

<style lang="less" scoped>
</style>
